package at.fhooe.tuxgestures.cam;

import java.io.File;
import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

import at.fhooe.tuxgestures.cam.data.CameraDataProvider;
import at.fhooe.tuxgestures.cam.data.ICameraDataProvider;
import at.fhooe.tuxgestures.cam.data.event.DataEvent;
import at.fhooe.tuxgestures.cam.data.event.IDataEventListener;
import at.fhooe.tuxgestures.cam.mapper.CameraDataMovementMapper;
import at.fhooe.tuxgestures.cam.mapper.ICameraDataMovementMapper;
import at.fhooe.tuxgestures.cam.mapper.event.IStateChangeEventListener;
import at.fhooe.tuxgestures.cam.mapper.event.StateChangeEvent;
import at.fhooe.tuxgestures.keysimulator.IKeySimulator.State;

public class CameraController implements ICameraController //, IStateChangedEventListener
{
	//private List<IStateChangedEventListener> listeners = new ArrayList<IStateChangedEventListener>();
	
	// Camera Data Provider (provides the matrices)
	ICameraDataProvider provider = null;
	
	// Maps the matrix to a state
	private ICameraDataMovementMapper mapper = null;
	private State previousState = State.IDLE; 
	
	private File dataFile = null;
	
	public CameraController()
	{
		// activate this if needed...
//		getMapper().addStateChangeListener(this);
	}
	
	public CameraController(File data)
	{
		this();
		this.dataFile = data;
	}
	
	private ICameraDataProvider getCameraDataProvider()
	{
		if (this.provider == null)
		{
			provider = new CameraDataProvider();
			provider.addListener(getMapper());
		}
		
		return provider;
	}
	
	private ICameraDataMovementMapper getMapper()
	{
		if (mapper == null)
			mapper = new CameraDataMovementMapper();
		
		return mapper;
	}
	
//	private void fireEvent(StateChangedEvent event)
//	{
//		for (IStateChangedEventListener listener : listeners)
//			listener.cameraEventHappened(event);
//	}

	@Override
	public void addCameraEventListener(IStateChangeEventListener listener)
	{
		getMapper().addStateChangeListener(listener);
	}

	@Override
	public void removeCameraEventListener(IStateChangeEventListener listener)
	{
		getMapper().removeStateChangeListener(listener);
	}

	@Override
	public void start()
	{
		if (dataFile != null)
			getCameraDataProvider().startReadFromFile(dataFile);
		else
			getCameraDataProvider().startReadFromCamera();
	}

	@Override
	public void stop()
	{
		getCameraDataProvider().stopReading();
	}
//
//	@Override
//	public void cameraEventHappened(StateChangedEvent event)
//	{		
//	}
	
	
	

}
